package ipdesk.model.report.seminar.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ipdesk.model.businessInfo.bean.BusinessInfoBean;
import ipdesk.model.report.seminar.bean.SeminarInfoBean;

import org.apache.commons.lang3.StringUtils;

import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.template.SqlMapDaoTemplate;
import com.opensymphony.xwork2.ActionContext;

import ecg.util.SessionUtils;
import ecg.util.SqlMapLocator;

public class SeminarInfoImpl extends SqlMapDaoTemplate implements ISeminarInfo
{
	
	public SeminarInfoImpl(DaoManager manager)
	{
		super(manager);	
	}
	
	public int seminarInfoReg(SeminarInfoBean seminarInfoBean) throws Exception
	{
		int seminarCodeCount = 0;
		int lastSeminarSeq = 0;
		String seminarCode = "";


			
		String startDate = seminarInfoBean.getStartDate();
		String deskCode = seminarInfoBean.getDeskCode();

		// 관리코드를 생성하기 위해 등록날짜와 데스크 코드를 조건으로 한 데이터의 갯수를 가지고온다.
		seminarCodeCount = ((Integer)this.queryForObject("seminar.seminarCode-count", seminarInfoBean)).intValue();
				
		String endNumber = StringUtils.leftPad(Integer.toString(seminarCodeCount+1), 4, "0");

		// 생성된 침해조사일지 관리코드.
		seminarCode = startDate.replaceAll("-","") + "-" + deskCode.substring(4) + "-SMN-" + endNumber;

		
		// 생성된 침해조사일지 관리코드 빈 객체에 저장.
		seminarInfoBean.setManageNo(seminarCode);

		seminarInfoBean.setUserSeq(Integer.parseInt(SessionUtils.getLoginSession().get("userSeq")));
		
		// iBatis 연동. 데이터 insert.
		lastSeminarSeq = ((Integer)this.insert("seminar.seminarInfoInsert", seminarInfoBean)).intValue();
					

		return lastSeminarSeq;
	}
	
	public void insertFileData(SeminarInfoBean seminarInfoBean)
	{
		// iBatis 연동. 데이터 insert.
		this.insert("seminar.seminarFileInfoInsert", seminarInfoBean);
	}
	
	public int getDataTotalCount(SeminarInfoBean seminarInfoBean) throws Exception
	{
		
		
		
		Map map = new HashMap();
		map.put("userSeq", SessionUtils.getLoginSession().get("userSeq"));
		map.put("authCode", SessionUtils.getLoginSession().get("authCode"));
		//if(seminarInfoBean.getDeskCode() == null || "".equals(seminarInfoBean.getDeskCode())){
		//	map.put("deskCode", SessionUtils.getLoginSession().get("deskCode"));
		//}else{
			map.put("deskCode", seminarInfoBean.getDeskCode());
		//}
	
		//seminarInfoBean.setDeskCode(SessionUtils.getLoginSession().get("deskCode"));
		
		int totalDataCount = ((Integer)this.queryForObject("seminar.seminarInfoTotalcount", map)).intValue();
		
		return totalDataCount;
	}

	
	public List<SeminarInfoBean> getPagingDataListForUser(Map map) throws Exception {
		map.put("userSeq", SessionUtils.getLoginSession().get("userSeq"));
		map.put("authCode", SessionUtils.getLoginSession().get("authCode"));
		
		List<SeminarInfoBean> pageDataList = this.queryForList("seminar.seminarPageDataListForUser", map);
		
		return pageDataList;
	}
	
	public SeminarInfoBean getDetailData(int seminarSeq)
	{
		SeminarInfoBean seminarInfoBean = (SeminarInfoBean)this.queryForObject("seminar.seminarDetailData", seminarSeq);
		
		return seminarInfoBean;
		
	}
	
	public void delFileData(String fileSeq)
	{
		this.delete("seminar.seminarDelFileData", fileSeq);
	}
	
	public void updateDelFileData(String fileSeq)
	{
		this.update("seminar.updateDelFileData", fileSeq);
	}
	
	public void updateDelPicData(String fileSeq)
	{
		this.update("seminar.updateDelPicData", fileSeq);
	}
	
	public void delDetailData(int seminarSeq)
	{
		this.update("seminar.seminarDelDetailData", seminarSeq);
	}
	
	public void seminarInfoUpdate(SeminarInfoBean seminarInfoBean)
	{
		this.update("seminar.seminarInfoUpdate", seminarInfoBean);
	}
	
	public void insertFileData2(SeminarInfoBean seminarInfoBean)
	{
		// iBatis 연동. 데이터 insert.
		this.insert("seminar.seminarFileInfoInsert2", seminarInfoBean);
	}
	
	public void updateFileData(SeminarInfoBean seminarInfoBean)
	{
		this.update("seminar.updateFileData", seminarInfoBean);
	}

	/**
	 * 파일 상세보기
	 */
	public SeminarInfoBean getFileDetailData(SeminarInfoBean seminarInfoBean) {
		// TODO Auto-generated method stub
		return (SeminarInfoBean)this.queryForObject("seminar.fileDetailData", seminarInfoBean);
	}
}
